package com.tc.web;

import com.fasterxml.jackson.annotation.JsonView;
import com.tc.dto.User;
import com.tc.dto.UserQueryCondition;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.builder.ReflectionToStringBuilder;
import org.apache.commons.lang.builder.ToStringStyle;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.annotation.Id;
import org.springframework.data.domain.Pageable;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.ServletRequestUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;

/**
 * @Auther: tianchao
 * @Date: 2020/6/21 21:31
 * @Description:
 */
@RestController
@RequestMapping("/user")
@Api(tags = "用户控制器")
public class UserController {

    private Logger logger = LoggerFactory.getLogger(UserController.class);

    @GetMapping("/me")
    public Object getCurrentUser(){
        return SecurityContextHolder.getContext().getAuthentication();
    }

    @GetMapping("/meDetails")
    public Object getCurrentUser(@AuthenticationPrincipal UserDetails userDetails){
        return userDetails;
    }

    @GetMapping("/session")
    public String session(String key, HttpSession session){
        return (String) session.getAttribute(key);
    }

    @GetMapping
    @JsonView(User.UserSimpleView.class)
    public List<User> query(UserQueryCondition condition, Pageable pageable){
        System.out.println(condition);
        System.out.println(ReflectionToStringBuilder.toString(condition, ToStringStyle.MULTI_LINE_STYLE));
        System.out.println(pageable.getClass());
        System.out.println(pageable.getPageSize());
        System.out.println(pageable.getPageNumber());
        System.out.println(pageable.getSort());
        List<User> users = new ArrayList<>();
        User user1 = new User();
        user1.setUsername("1");
        user1.setPassword("123");
        users.add(user1);
        users.add(new User());
        users.add(new User());
        return users;
    }
    @GetMapping(value = "/{id:\\d+}")
    @JsonView(User.UserDetailView.class)
    public User query(@PathVariable("id") String id){
        logger.info("id为:" + id);
        User user = new User();
        user.setUsername("Tom");
        user.setPassword("123456");
        return user;
    }
    @ApiOperation(value = "创建用户")
    @PostMapping
    public User create(@Valid @RequestBody User user, BindingResult bindingResult){
        if (bindingResult.hasErrors()){
            bindingResult.getAllErrors().stream().forEach(error->{
                System.out.println(error.getDefaultMessage());
            });
        }
        user.setId(123L);
        logger.info("id为:" + user.toString());
        return user;
    }

    @DeleteMapping(value = "/{id}")
    public void delete(@PathVariable("id") Long id){
        logger.info("delete id : " + id);
    }

    @PutMapping
    public void update(@Valid @RequestBody User user){
        logger.info("update 为:" + user.toString());
    }
}
